package com.yueya.api.upload;

import java.io.File;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;
import com.yueya.common.action.ApiController;
import com.yueya.common.util.UploadUtil;
import com.yueya.config.GlobalParam;

public class UploadController extends ApiController {

	// 文件上传
	public void uploadfile() {
		Map<String, Object> uploadMap = null;
		UploadFile upfile = getFile();

		if (StrKit.notNull(upfile)) {
			File file = upfile.getFile();

			String filename = file.getName();
			String filedir = "/other"; // 存放目录
			String[] filenames = filename.split("\\.");
			if ("jpg,jpeg,gif,png".indexOf(filenames[filenames.length - 1]) != -1) {
				filedir = "/image";
			} else if ("doc,docx,xls,xlsx,pdf".indexOf(filenames[filenames.length - 1]) != -1) {
				filedir = "/document";
			}

			uploadMap = UploadUtil.uploadFile(GlobalParam.getAttUploadSavePath(), upfile.getFileName(),
					filedir, file);

			if ("1".equals(uploadMap.get("flag").toString())) {
				String savePath = (String) uploadMap.get("savepath");
				uploadMap.put("savepath", GlobalParam.getAttUrl() + savePath);
				uploadMap.put("relativepath", savePath);
				uploadMap.put("filename", upfile.getFileName());
			}

		}
		renderJson(uploadMap);
	}

	public static void main(String args[]) {
		String a = "jpg,png";
		if (a.indexOf("jpg") != -1) {
			System.out.println("have");
		} else {
			System.out.println("no have");
		}
	}
}
